package tech.aister.util;

import io.jsonwebtoken.Claims;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import teach.aistar.mapper.UserMapper;
import teach.aistar.pojo.User;

import java.util.Date;

/**
 * 本来用来演示:
 *
 * @author success
 * @date 2025/3/4 09:17:17
 */
@SpringBootTest
public class TokenTest {
    @Autowired
    private UserMapper userMapper;

    @Test
    public void testCreateToken(){
        //模拟一个登录成功的用户
        User user = userMapper.getByEmail("2422368325@qq.com");
        String token = JWTUtils.geneJsonWebToken(user);
        //aistareyJhbGciOiJIUzI1NiJ9
        //.eyJzdWIiOiJsb2dpbiIsImlkIjoxLCJ1c2VybmFtZSI6ImFkbWluIiwiaWF0IjoxNzQxMDUxMTUyLCJleHAiOjE3NDE2NTU5NTJ9
        //.EslzHeT3l_YnASb74j0kqCU871x7PlxvOftkJ63tlnc
        System.out.println(token);

    }

    @Test
    public void testCheckToken(){
        //模拟的是后续的请求再次携带token发送到server端
        Claims claim = JWTUtils.checkJWT("aistareyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJsb2dpbiIsImlkIjoxLCJ1c2VybmFtZSI6ImFkbWluIiwiaWF0IjoxNzQxMDUxNTI0LCJleHAiOjE3NDEwNTE1NjR9.J1Jp83pZtzf1ikZNJxTZ5yot2d6EFwaGUPgMAoWvHQM");
        //1. 非法的token,返回null
        //2. 过期的token,返回null
        System.out.println("claims:"+claim);

        if(null!=claim) {
            //2. 根据key获取登录用户的有效信息
            Integer userId = (Integer) claim.get("id");
            String username = (String) claim.get("username");

            System.out.println(userId + ":" + username);
        }
    }
}
